// pages/rescue/rescue.js
var bmap = require('../../libs/bmap-wx.min.js');
var wxMarkerData = [];
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id: 0,
		destination: {
			'map': '请输入拖车目的地'
		},
		tc: {
			millions: 0,
			price: 0,
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.map()
		let that = this
	},
	map() {
		let that = this
		var BMap = new bmap.BMapWX({
			ak: 'GSEHM6rV9bIquAHVUuLLxYHTThvGAW5l'
		});
		var success = function(data) {
			console.log(data)
			wxMarkerData = data.wxMarkerData;
			that.setData({
				city: data.originalData.result.addressComponent.city
			});
			that.setData({
				lat1: wxMarkerData[0].latitude
			});
			that.setData({
				lng1: wxMarkerData[0].longitude
			});
			that.setData({
				map: data.originalData.result.formatted_address,
				markers: [{
					'latitude': wxMarkerData[0].latitude,
					'longitude': wxMarkerData[0].longitude,
					'id': '1'
				}]
			})
		}
		BMap.regeocoding({
			success: success,
		});
	},
	// 切换
	id(e) {
		this.setData({
			id: e.currentTarget.dataset.id
		})
	},
	onShow() {
		console.log(this.data.destination)
		this.distance()
	},
	regeocoding() {
		const that = this;
		bmap.regeocoding({
			location: that.data.latitude + ',' + that.data.longitude,
			success: function(res) {
				that.setData({
					address: res.wxMarkerData[0].address
				})
			},
			fail: function(res) {
				that.tipsModal('请开启位置服务权限并重试!')
			},

		});
	},
	regionchange(e) {
		// 地图发变化的时候，获取中间点，也就是用户选择的位置toFixed
		if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
			var that = this;
			this.mapCtx = wx.createMapContext("map4select");
			this.mapCtx.getCenterLocation({
				type: 'gcj02',
				success: function(res) {
					console.log(res)
					var BMap = new bmap.BMapWX({
						ak: 'GSEHM6rV9bIquAHVUuLLxYHTThvGAW5l'
					});
					BMap.regeocoding({
						location: res.latitude + ',' + res.longitude,
						success: function(res) {
							console.log(res)
							that.setData({
								address: res.wxMarkerData[0].address
							})
							var success = function(data) {
								wxMarkerData = data.wxMarkerData;
								that.setData({
									lat1: res.wxMarkerData[0].latitude
								});
								that.setData({
									lng1: res.wxMarkerData[0].longitude
								});
								that.setData({
									map: res.wxMarkerData[0].address,
									markers: [{
										'latitude': res.wxMarkerData[0].latitude,
										'longitude': res.wxMarkerData[0].longitude,
										'id': '1'
									}]
								})
							}
							BMap.regeocoding({
								success: success,
							});
						}
					})
				}
			})
		}
	},
	// 计算拖车距离
	distance() {
		if (this.data.destination.latitude != undefined) {
			let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
			let x = this.data.lng1;
			let y = this.data.lat1;
			let x1 = this.data.destination.longitude;
			let y1 = this.data.destination.latitude;
			let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
			let z1 = Math.sqrt(x1 * x1 + y1 * y1) + 0.00002 * Math.sin(y * x_pi);
			let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
			let lngs = z * Math.cos(theta) + 0.0065;
			let lats = z * Math.sin(theta) + 0.006;
			let lngs1 = z1 * Math.cos(theta) + 0.0065;
			let lats1 = z1 * Math.sin(theta) + 0.006;
			wx.request({
				url: getApp().globalData.http + 'action=jsMillions&lgt=' + lngs + '&lat=' + lats +
					'&Tlgt=' + lngs1 + '&Tlat=' + lats1,
				success: (e) => {
					console.log(e)
					e.data.data.millions = parseFloat(e.data.data.millions).toFixed(2)
					this.setData({
						tc: e.data.data
					})
					console.log(this.data.tc)
				}
			})
		}
	}
})
